package br.com.netomarin.bundesliga.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Scanner;

import br.com.netomarin.bundesliga.model.MatchModel;
import br.com.netomarin.bundesliga.model.PlayerModel;
import br.com.netomarin.bundesliga.persistence.Match;


public class MatchParser {

	public static void processMatchesCSV(InputStream matchIS, boolean initialData) throws IOException {
		ArrayList<Match> matches = new ArrayList<Match>();
		Match m;
		Scanner scanner = new Scanner(matchIS, "UTF-8");
        String line = null;
        while(scanner.hasNextLine()) {
        	line = scanner.nextLine();
        	if (line.startsWith("#"))
        		continue;
        	
        	m = MatchModel.loadMatchFromCSV(line);
        	PlayerModel.processPlayersMatch(m, initialData);
        	matches.add(m);
        }
        
        MatchModel.saveMatches(matches);
	}
}